package
{
	import demo.car.Bridge;
	import demo.car.Capsule;
	import demo.car.Car;
	import demo.car.Rotator;
	import demo.car.Surfaces;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import org.cove.ape.APEngine;
	import org.cove.ape.Vector2D;
	import org.cove.ape.WheelParticle;

	public class UnitTest extends Sprite
	{
		private static var colA:uint = 0x334433;
		private static var colB:uint = 0x3366aa;
		private static var colC:uint = 0xaabbbb;
		private static var colD:uint = 0x6699aa;
		private static var colE:uint = 0x778877;
		
		private var car:Car;
		private var rotator:Rotator;
		
		[SWF(width="650", height="350", backgroundColor="#334433")] 
		public function UnitTest()
		{
			super();
			
			stage.frameRate = 55;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			addEventListener(Event.ENTER_FRAME, run);
			
			var sp:Sprite = new Sprite();
			sp.graphics.beginFill(0x550033, 1.0);
			sp.graphics.drawRect(0, 0, 10, 10);
			sp.graphics.endFill();
			// Initialize the engine. The argument here is the time step value. 
			// Higher values scale the forces in the sim, making it appear to run
			// faster or slower. Lower values result in more accurate simulations.
			APEngine.init(1/4);
			
			// set up the default diplay container
			APEngine.container = this;
			
			// gravity -- particles of varying masses are affected the same
			APEngine.addMasslessForce(new Vector2D(0, 3));
			
			var surfaces:Surfaces = new Surfaces(colA, colB, colC, colD, colE);
			APEngine.addGroup(surfaces);
			
			rotator = new Rotator(colB, colE);
			APEngine.addGroup(rotator);			
			
			var bridge:Bridge = new Bridge(colB, colC, colD);
			APEngine.addGroup(bridge);
			
			var capsule:Capsule = new Capsule(colC);
			APEngine.addGroup(capsule);
			
			car = new Car(colC, colE);
			APEngine.addGroup(car);			
			
			car.addCollidableList(new Array(surfaces, bridge, rotator/* , swingDoor */, capsule));
			capsule.addCollidableList([surfaces, bridge, rotator/* , swingDoor */]);
			
			var whel:WheelParticle = new WheelParticle(50, 50, 10, false, 10, 0.01, 0.09);
			car.addParticle(whel);
			whel.sprite.addChild(sp);
			
		}
		
		private function keyDownHandler(e:KeyboardEvent):void
		{
			
		}
		
		private function keyUpHandler(e:KeyboardEvent):void
		{
			
		}
		
		private function run(e:Event):void
		{
			APEngine.step();
			APEngine.paint();
			rotator.rotateByRadian(.02);
		}
	}
}